const db = wx.cloud.database().collection("tiku")
const user = wx.cloud.database().collection("users")
const app = getApp()
var suijishu=0
var suijishuzu=[]
var lj=0//积分
var ans=''
var rig=0//正确个数
var wrong=0
var xi=1//目前题目编号
var times=0
Page({
  data: {
    muqian:1,
    a:'A',
    b:'B',
    c:'C',
    notapa:false,
    notapb:false,
    notapc:false
  },
  //结算
  jiesuan:function(){
    var _id=0
    var jf=0
    if(rig===5) lj++
    user.where({
      _openid:app.globalData.openid
    }).get({
      success(res){
        _id=res.data[0]._id
        jf=res.data[0].mark
        jf+=lj
        user.doc(_id).update({
          data:{
            mark:jf
          },
        })
      },
    })
    setTimeout(function(){
    wx.showModal({
      title: '挑战成功',
      content: '正确题数：'+String(rig)+' / 5，获得积分：'+String(lj)+'，总积分：'+String(jf),
      showCancel:false,
      success(res){
        if(res.confirm){
          wx.navigateBack({})
          rig=0
          wrong=0
          xi=1
        }
      }
    })},1000)
  },
  tapa:function(){
    clearInterval(times);
    var that=this
    if(ans==='A'){
      lj+=1
      rig++
      xi++
      that.setData({
        scorea:"#7fbd3e",
      })
    } else{
      wrong++
      xi++
      if(ans==='B'){
        that.setData({
          scoreb:"#7fbd3e",
          scorea:"#cc4040",
        })
      } else{
        that.setData({
          scorec:"#7fbd3e",
          scorea:"#cc4040",
        })
      }
    }
    that.setData({
      notapa:true,
      notapb:true,
      notapc:true
    })
    if(xi>=6) that.jiesuan()
    else {
      setTimeout(function(){
        that.setData({
          scorea:"#fff",
          scoreb:"#fff",
          scorec:"#fff",
          muqian:xi,
        })
        getCurrentPages()[getCurrentPages().length - 1].onLoad()
      },2000)
    }
  },
  tapb:function(){
    clearInterval(times);
    var that=this
    if(ans==='B'){
      lj+=1
      rig++
      xi++
      that.setData({
        scoreb:"#7fbd3e",
      })
    } else{
      wrong++
      xi++
      if(ans==='A'){
        that.setData({
          scorea:"#7fbd3e",
          scoreb:"#cc4040",
        })
      } else{
        that.setData({
          scorec:"#7fbd3e",
          scoreb:"#cc4040",
        })
      }
    }
    that.setData({
      notapa:true,
      notapb:true,
      notapc:true
    })
    if(xi>=6) that.jiesuan()
    else {
      setTimeout(function(){
        that.setData({
          scorea:"#fff",
          scoreb:"#fff",
          scorec:"#fff",
          muqian:xi,
        })
        getCurrentPages()[getCurrentPages().length - 1].onLoad()
      },2000)
    }
    
  },
  tapc:function(){
    clearInterval(times);
    var that=this
    if(ans==='C'){
      lj+=1
      rig++
      xi++
      that.setData({
        scorec:"#7fbd3e",
      })
    } else{
      wrong++
      xi++
      if(ans==='A'){
        that.setData({
          scorea:"#7fbd3e",
          scorec:"#cc4040",
        })
      } else{
        that.setData({
          scoreb:"#7fbd3e",
          scorec:"#cc4040",
        })
      }
    }
    that.setData({
      notapa:true,
      notapb:true,
      notapc:true
    })
    if(xi>=6) that.jiesuan()
    else {
      setTimeout(function(){
        that.setData({
          scorea:"#fff",
          scoreb:"#fff",
          scorec:"#fff",
          muqian:xi,
        })
        getCurrentPages()[getCurrentPages().length - 1].onLoad()
      },2000)
    }
    
  },
  drawProgressbg: function () {
    // 使用 wx.createContext 获取绘图上下文 context
    var ctx = wx.createCanvasContext('canvasProgressbg')
    ctx.setLineWidth(12);// 设置圆环的宽度
    ctx.setStrokeStyle('#FFFFFF'); // 设置圆环的颜色
    ctx.setLineCap('round') // 设置圆环端点的形状
    ctx.beginPath();//开始一个新的路径
    ctx.arc(42, 42, 30, 0, 2 * Math.PI, false);
    //设置一个原点(100,100)，半径为90的圆的路径到当前路径
    ctx.stroke();//对当前路径进行描边
    ctx.draw();
  },
  onUnload:function(){
    suijishu=0
    suijishuzu=[]
    lj=0//积分
    ans=''
    rig=0//正确个数
    wrong=0
    xi=1//目前题目编号
    times=0
  },
  onLoad: function (options) {
    var that=this
    db.count().then(res => {
      do{
        suijishu=parseInt(Math.random()*(res.total));
        }while(!(suijishuzu.indexOf(suijishu)===-1))
        suijishuzu.push(suijishu)
        wx.cloud.callFunction({
          name:"getqus",
          data:{
            x:suijishu
          },
          success(res){
            ans=res.result.data[0].ANS
            that.setData({
              question:res.result.data[0].QUS,
              a:res.result.data[0].A,
              b:res.result.data[0].B,
              c:res.result.data[0].C
            })
            // 倒计时前先绘制整圆的圆环
            ringMove(start, end);
            // 创建倒计时
            time = setInterval(animation, animation_interval);
            times=time
          }
        })
    })
    
    var step = 1,//计数动画次数
      num = 0,//计数倒计时秒数（n - num）
      start = 1.5 * Math.PI,// 开始的弧度
      end = -0.5 * Math.PI,// 结束的弧度
      time = null;// 计时器容器

    var animation_interval = 1000,// 每1秒运行一次计时器
      n = 10; // 当前倒计时为5秒
    // 动画函数
    function animation() {
      if (step <= n) {
        end = end + 2 * Math.PI / n;
        ringMove(start, end);
        step++;
      } else {//到时间
        clearInterval(time);
        wrong++;
        xi++;
        that.setData({
          notapa:true,
          notapb:true,
          notapc:true
        })
        if(ans==='A'){
          that.setData({
            scorea:"#7fbd3e",
          })
        } else if(ans==='B'){
          that.setData({
            scoreb:"#7fbd3e",
          })
        } else{
          that.setData({
            scorec:"#7fbd3e",
          })
        }
        setTimeout(function(){
          that.setData({
            scorea:"#fff",
            scoreb:"#fff",
            scorec:"#fff",
          })
          if(xi>=6) that.jiesuan()
          else {
            that.setData({
              muqian:xi
            })
            getCurrentPages()[getCurrentPages().length - 1].onLoad()
          }
        },2000)
      }
    };
    // 画布绘画函数
    function ringMove(s, e) {
      var context = wx.createCanvasContext('secondCanvas')

      var gradient = context.createLinearGradient(200, 100, 100, 200);
      gradient.addColorStop("0", "#2661DD");
      gradient.addColorStop("0.5", "#40ED94");
      gradient.addColorStop("1.0", "#5956CC");

      // 绘制圆环
      context.setStrokeStyle('#f3a74f')
      context.beginPath()
      context.setLineWidth(10)
      context.arc(42, 42, 30, s, e, true)
      context.stroke()
      context.closePath()

      // 绘制倒计时文本
      context.beginPath()
      context.setLineWidth(1)
      context.setFontSize(25)
      context.setFillStyle('#ffffff')
      context.setTextAlign('center')
      context.setTextBaseline('middle')
      context.fillText(n - num + '', 42, 42, 30)
      context.fill()
      context.closePath()

      context.draw()

      // 每完成一次全程绘制就+1
      num++;
    }
    //button abled
    setTimeout(() => {
      that.setData({
        notapa:false,
        notapb:false,
        notapc:false
      })
    }, 250);
  },

  onReady: function () {
    this.drawProgressbg(); 
  },
})